﻿using PARK.Common.Core.Manager;
using PARK.Common.Enum;
using PARK.Common.Extension;
using PARK.Common.Filter;
using PARK.Dependency;
using PARK.DynamicApiController;
using PARK.FriendlyException;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Yitter.IdGenerator;
using PARK.Common.Helper;
using PARK.JsonSerialization;
using PARK.CardAcct.Interfaces;
using PARK.CardAcct.Entitys.Dto.employee;
using PARK.System.Interfaces.Permission;
using PARK.Common.Util;
using PARK.System.Entitys.Permission;
using System.Text;
using PARK.CardAcct.Entitys;
using PARK.CardAcct.Entitys.Dto.Wechat;
using Microsoft.AspNetCore.Authorization;
using PARK.Attend.Entitys;
using PARK.Accessctl.Entitys.Dto.AccessRecord;

namespace PARK.CardAcct.Attend
{
    /// <summary>
    /// 小程序用户
    /// </summary>
    [ApiDescriptionSettings(Tag = "Wechat", Name = "AttendRecordUser", Order = 200)]
    [Route("api/wechat/[controller]")]
    public class AttendRecordUserService : IWechatUserService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<AttendRecordEntity> _baseEmployeeRepository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;
        private readonly IOrganizeService _organizeService; // 机构表仓储

        /// <summary>
        /// 初始化一个<see cref="AttendRecordUserService"/>类型的新实例
        /// </summary>
        public AttendRecordUserService(
            ISqlSugarRepository<AttendRecordEntity> baseEmployeeRepository,
            IUserManager userManager,
            IOrganizeService organizeService)
        {
            _baseEmployeeRepository = baseEmployeeRepository;
            _db = _baseEmployeeRepository.Context;
            _userManager = userManager;
            _organizeService = organizeService;
        }

        #region 获取用户考勤记录
        /// <summary>
        /// 获取用户考勤记录
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAttendDetInfo")]
        public async Task<dynamic> GetAttendDetInfo([FromQuery] AttendRecordListQueryInput input)
        {
            var userInfo = await _userManager.GetUserInfo();
            var _EmployeeId = "0";
            if (userInfo != null && userInfo.UserKind == 1 && !string.IsNullOrEmpty(userInfo.EmployeeId))
            {
                _EmployeeId = userInfo.EmployeeId;
                var sidx = input.sidx == null ? "sortCode" : input.sidx;
                //打卡日期
                List<string> queryBillDate = input.passtime != null ? input.passtime.Split(',').ToObeject<List<string>>() : null;
                DateTime? startBilldate = queryBillDate != null ? Convert.ToDateTime(queryBillDate[0]) : Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                DateTime? endBilldate = queryBillDate != null ? Convert.ToDateTime(queryBillDate[0]) : Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                var data = await _db.Queryable<AttendRecordEntity>()
                    .Where(p => p.EmployeeId.Equals(_EmployeeId))
                    //打卡日期
                    .WhereIF(queryBillDate != null, a => Convert.ToDateTime(a.SignTime) >= new DateTime(startBilldate.ToDate().Year, startBilldate.ToDate().Month, startBilldate.ToDate().Day, 0, 0, 0))
                    .WhereIF(queryBillDate != null, a => Convert.ToDateTime(a.SignTime) <= new DateTime(endBilldate.ToDate().Year, endBilldate.ToDate().Month, endBilldate.ToDate().Day, 23, 59, 59))
                    .Select(it => new AttendRecordListOutput
                    {
                        id = it.Id,
                        accessreId = it.accessreId,
                        worktype = it.worktype,
                        signtype = it.SignType,
                        signstatus = it.SignStatus,
                        signmint = it.SignMint,
                        ctlId = it.ctlId,
                        ctlSn = it.ctlSn,
                        ctlName = it.ctlName,
                        employeeId = it.EmployeeId,
                        jobNo = it.JobNo,
                        reallName = it.ReallName,
                        departId = it.DepartId,
                        departName = it.DepartName,
                        cardNo = it.CardNo,
                        signtime = Convert.ToDateTime(it.SignTime).ToString("yyyy-MM-dd HH:mm:ss"),
                        sortCode = it.SortCode
                    }).MergeTable().OrderBy(sidx + " asc ").ToPagedListAsync(input.currentPage, input.pageSize);
                return PageResult<AttendRecordListOutput>.SqlSugarPageResult(data);
            }
            else
                return null;
        }
        #endregion
    }
}
